package owg.util.data;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterOutputStream;

import owg.util.Calc;

public class Compression
	{
	public static byte[] compress(float[] data)
		{
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		DeflaterOutputStream compressorStream = new DeflaterOutputStream(byteOut);
		try {ByteBuffer buf = ByteBuffer.allocate(data.length*Calc.bytesPerFloat);
			buf.asFloatBuffer().put(data);
			compressorStream.write(buf.array());
			compressorStream.close();}
		catch (IOException e)
			{throw new RuntimeException("Compression Failed!");}
		return byteOut.toByteArray();
		}
	public static float[] decompress(byte[] data)
		{
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		InflaterOutputStream decompressorStream = new InflaterOutputStream(byteOut);
		try {decompressorStream.write(data); decompressorStream.close();}
		catch (IOException e)
			{throw new RuntimeException("Decompression Failed!");}
		FloatBuffer floats = ByteBuffer.wrap(byteOut.toByteArray()).asFloatBuffer();
		float[] result = new float[floats.capacity()];
		floats.get(result);
		return result;
		}
	}
